package com.fr.lintcode;

import org.junit.Test;

public class Q179 {
    /**
     *@param n, m: Two integer
     *@param i, j: Two bit positions
     *return: An integer
     */
    public int updateBits(int n, int m, int i, int j) {
        int p = 1;
        for (int k = 31; k > j; k--) {
            p = (p << 1) | 1;
        }
        for (int k = i; k <= j; k++) {
            p = (p << 1);
        }
        for (int k = 0; k < i; k++) {
            p = (p << 1) | 1;
        }
        return (n & p) | (m << i);
    }

    @Test
    public void testA() {
        // 1024,21,2,6
        // -2147483648, -1, 0, 30
        // -2147483648,2147483647,0,30
        System.out.println(updateBits(-1, 2, 0, 30));
    }
}
